/**
 * sequelize实例
 */
const Sequelize = require('sequelize')
const { MYSQL_CONF } = require('../config/db')//MYSQL_CONF的配置
const { isProd, isTest } = require('../utils/env')
const { MYSQLPSD, MYSQLNAME } = require('../config/constant');

const { host, user, timezone, dialectOptions, port, password, database } = MYSQL_CONF

const conf = {
    host,
    user,
    timezone,
    dialectOptions,
    port,
    password,
    database,
    dialect: 'mysql',
}

if (isTest) {
    conf.logging = () => { }
}
//创建数据库连接池---线上环境，使用连接池
if (isProd) {
    conf.pool = {
        max: 5,//连接池最大的连接数量
        min: 0,//最小
        idle: 10000 //如果一个连接池10秒内没有被使用，则释放
    }
}

const seq = new Sequelize(MYSQLNAME, 'root', MYSQLPSD, conf)

module.exports = seq